<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Splits given source into PHP tokens, represented by PhpToken objects.</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="phptoken.tostring.html">« PhpToken::__toString</a></li>
      <li style="float: right;"><a href="ref.tokenizer.html">Tokenizer 函数 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.phptoken.html">PhpToken</a></li>
    <li>Splits given source into PHP tokens, represented by PhpToken objects.</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="phptoken.tokenize" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PhpToken::tokenize</h1>
  <p class="verinfo">(PHP 8)</p><p class="refpurpose"><span class="refname">PhpToken::tokenize</span> &mdash; <span class="dc-title">Splits given source into PHP tokens, represented by PhpToken objects.</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-phptoken.tokenize-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><strong>PhpToken::tokenize</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$code</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>): <span class="type">array</span></div>

  <p class="para rdfs-comment">
   Returns an array of PhpToken objects representing given <code class="parameter">code</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-phptoken.tokenize-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
     <dt>
<code class="parameter">code</code></dt>

     <dd>

      <p class="para">
       The PHP source to parse.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">flags</code></dt>

     <dd>

      <p class="para">
       Valid flags:
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara">
          <strong><code>TOKEN_PARSE</code></strong> - Recognises the ability to use
          reserved words in specific contexts.
         </span>
        </li>
       </ul>
      </p>
     </dd>

    
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-phptoken.tokenize-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   An array of PHP tokens represented by instances of PhpToken or its descendants.
   This method returns <span class="type"><span class="type static[]">static[]</span></span> so that PhpToken can be seamlessly extended.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-phptoken.tokenize-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-3830">
   <p><strong>示例 #1 <span class="function"><strong>PhpToken::tokenize()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$tokens&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">PhpToken</span><span style="color: #007700">::</span><span style="color: #0000BB">tokenize</span><span style="color: #007700">(</span><span style="color: #DD0000">'&lt;?php&nbsp;echo;&nbsp;?&gt;'</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$tokens&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$token</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Line&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$token</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">line</span><span style="color: #007700">}</span><span style="color: #DD0000">:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$token</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getTokenName</span><span style="color: #007700">()}</span><span style="color: #DD0000">&nbsp;('</span><span style="color: #007700">{</span><span style="color: #0000BB">$token</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">text</span><span style="color: #007700">}</span><span style="color: #DD0000">')"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Line 1: T_OPEN_TAG (&#039;&lt;?php &#039;)
Line 1: T_ECHO (&#039;echo&#039;)
Line 1: ; (&#039;;&#039;)
Line 1: T_WHITESPACE (&#039; &#039;)
Line 1: T_CLOSE_TAG (&#039;?&gt;&#039;)
</pre></div>
   </div>
  </div>
  <div class="example" id="example-3831">
   <p><strong>示例 #2 Extending PhpToken</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyPhpToken&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">PhpToken&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">getUpperText</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">strtoupper</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">text</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$tokens&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">MyPhpToken</span><span style="color: #007700">::</span><span style="color: #0000BB">tokenize</span><span style="color: #007700">(</span><span style="color: #DD0000">'&lt;?php&nbsp;echo;&nbsp;?&gt;'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"'</span><span style="color: #007700">{</span><span style="color: #0000BB">$tokens</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">getUpperText</span><span style="color: #007700">()}</span><span style="color: #DD0000">'"</span><span style="color: #007700">;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
&#039;&lt;?PHP &#039;
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-phptoken.tokenize-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="function.token-get-all.html" class="function" rel="rdfs-seeAlso">token_get_all()</a> - 将提供的源码按 PHP 标记进行分割</span></li>
  </ul>
 </div>

</div></div></div></body></html>